package com.xxc.hs;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.xxc.hs.bean.CloudInfo;
import com.xxc.hs.bean.UserInfo;
import com.xxc.hs.db.UserHelper;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

@SuppressWarnings("ALL")
public class LoginActivity extends AppCompatActivity {
    private EditText et_name, et_pwd;
    private CheckBox checkBox;
    private TextView tv_register;
    private boolean login_flag;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        try {
            CloudInfo.setCloudService();

            initView();
            sharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
            login_flag = sharedPreferences.getBoolean("is_login", false);

            if (login_flag) {
                String username = sharedPreferences.getString("name", null);
                String userpwd = sharedPreferences.getString("pwd", null);
                et_name.setText(username);
                et_pwd.setText(userpwd);
                checkBox.setChecked(true);
            }

            findViewById(R.id.btn_login).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String name = et_name.getText().toString();
                    String pwd = et_pwd.getText().toString();

                    if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) {
                        Toast.makeText(LoginActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                    } else {
                        UserInfo userInfo = UserHelper.getInstance(LoginActivity.this).login(name);
                        if (null != userInfo) {
                            if (name.equals(userInfo.getUsername()) && pwd.equals(userInfo.getPassword())) {
                                SharedPreferences.Editor editor = sharedPreferences.edit();
                                editor.putBoolean("is_login", login_flag);
                                editor.putString("name", name);
                                editor.putString("pwd", pwd);
                                editor.apply();

                                UserInfo.setUserInfo(userInfo);

                                if (userInfo.getType() == 0) {
                                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                                } else if (userInfo.getType() == 1) {
                                    Toast.makeText(LoginActivity.this, "欢迎您，管理员", Toast.LENGTH_SHORT).show();
                                }
                                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                                finish();
                            } else {
                                Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(LoginActivity.this, "账号未注册", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            });

            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    login_flag = isChecked;
                }
            });

            tv_register.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    startActivity(new Intent(LoginActivity.this, RegistActivity.class));
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initView() {
        et_name = findViewById(R.id.et_name);
        et_pwd = findViewById(R.id.et_pwd);
        checkBox = findViewById(R.id.checkbox_rember);
        tv_register = findViewById(R.id.tv_register);
    }

}
